package com.situ.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.ssm.entity.PageInfo;
import com.situ.ssm.entity.Result;
import com.situ.ssm.entity.Student;
import com.situ.ssm.mapper.StudentMapper;
import com.situ.ssm.service.IStudentService;

@Service
public class StudentServiceImpl implements IStudentService{
	@Autowired
	private StudentMapper studentMapper;

	
	@Override
	public Result findByPage(Integer page, Integer limit) {
		// 一共有多少数据，不是当前页的个数
		int count = studentMapper.getTotalCount();
		// 当前页的数据
		int offset = (page - 1) * limit;
		List<Student> list = studentMapper.findByPage(offset, limit);
		
		return Result.success("", list, count);
	}
	
	
	@Override
	public PageInfo findByPage2(Integer pageNo, Integer pageSize) {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(pageNo);
		pageInfo.setPageSize(pageSize);
		
		// totalPage 
		int count = studentMapper.getTotalCount();
		int totalPage = (int)Math.ceil((double)count / pageSize);
		pageInfo.setTotalPage(totalPage);
		
		// list
		// pageNo,pageSize-->limit (pageNo-1)*pageSize,pageSize
		int offset = (pageNo - 1) * pageSize;
		List<Student> list = studentMapper.findByPage(offset, pageSize);
		pageInfo.setList(list);
		
		return pageInfo;
	}

	@Override
	public boolean insert(Student student) {
		int count = studentMapper.insert(student);
		return count == 1 ? true : false;
	}


	@Override
	public boolean deleteById(Integer id) {
		int count = studentMapper.deleteById(id);
		return count == 1 ? true : false;
	}

	

}
